package com.product.controller;

import com.product.id.ItemIdShardingKeyGenerator;
import com.product.mapper.ItemMapper;
import com.product.pojo.Item;
import com.product.utils.ThreadLocalUtil;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @author Hanhuiyong
 * @version 1.0.0
 * @ClassName ItemController.java
 * @Description
 * @createTime 2021年09月11日 17:34:00
 */
@RestController
public class ItemController {

    @Resource
    ItemMapper itemMapper;

    @Resource
    private ItemIdShardingKeyGenerator keyGenerator;

    @RequestMapping("/product/addItem")
    public String addItem() {

        for (int i = 1; i < 12; i++) {
            Item item = new Item();
            Long merchantId = Long.valueOf(i);
            item.setMerchantId(merchantId);
            item.setMerchantSkuId(1L);
            item.setStoreId(1L);
            ThreadLocalUtil.merchantIdThreadLocal.set(merchantId);
            item.setState(1);
            item.setInventory(100);
            item.setPrice(new BigDecimal("123.45"));
            item.setItemId((Long) keyGenerator.generateKey());
            itemMapper.insert(item);
        }

        return "success";
    }

    @RequestMapping("/product/{itemId}")
    public String getById(@PathVariable("itemId") Long itemId) {

        Item item = itemMapper.selectById(itemId);
        return item == null ? "" : item.toString();
    }

}
